<?php
class IndexSaveEditAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('index','edit');
	}
	
	public function execute()
	{
		$id				= $this->context->post('id');
		$data			= $this->getValidData();
		
		//价格
		if($data['info']['con']['start_price']!='' || $data['info']['con']['end_price']!='')
		{
			if(!($data['info']['con']['start_price']!='' && $data['info']['con']['end_price']!=''))
			{
				$this->data['error_messages']['info#con#end_price']	= array('both'=>'两个只能是都填或者都不填');
			}
			else
			{
				if($data['info']['con']['start_price']>=$data['info']['con']['end_price'])
				{
					$this->data['error_messages']['info#con#end_price']	= array('lower'=>'起始价格必须小于最高价格');
				}
			}
		}
		//推广数量
		if($data['info']['con']['start_commissionNum']!='' || $data['info']['con']['end_commissionNum']!='')
		{
			if(!($data['info']['con']['start_commissionNum']!='' && $data['info']['con']['end_commissionNum']!=''))
			{
				$this->data['error_messages']['info#con#end_commissionNum']	= array('both'=>'两个只能是都填或者都不填');
			}
			else
			{
				if($data['info']['con']['start_commissionNum']>=$data['info']['con']['end_commissionNum'])
				{
					$this->data['error_messages']['info#con#end_commissionNum']	= array('lower'=>'起始价格必须小于最高价格');
				}
			}
		}
		//佣金比率
		if($data['info']['con']['start_commissionRate']!='' || $data['info']['con']['end_commissionRate']!='')
		{
			if(!($data['info']['con']['start_commissionRate']!='' && $data['info']['con']['end_commissionRate']!=''))
			{
				$this->data['error_messages']['info#con#end_commissionRate']	= array('both'=>'两个只能是都填或者都不填');
			}
			else
			{
				if($data['info']['con']['start_commissionRate']>=$data['info']['con']['end_commissionRate'])
				{
					$this->data['error_messages']['info#con#end_commissionRate']	= array('lower'=>'起始佣金比率必须小于最高佣金比率');
				}
			}
		}
		
		//卖家信用
		$level	= array('1heart'=>1,'2heart'=>2,'3heart'=>3,'4heart'=>4,'5heart'=>5,'1diamond'=>6,'2diamond'=>7,'3diamond'=>8,'4diamond'=>9,'5diamond'=>10,'1crown'=>11,'2crown'=>12,'3crown'=>13,'4crown'=>14,'5crown'=>15,'1goldencrown'=>16,'2goldencrown'=>17,'3goldencrown'=>18,'4goldencrown'=>19,'5goldencrown'=>20,);
		if(!(array_key_exists($data['info']['con']['start_credit'],$level) && array_key_exists($data['info']['con']['end_credit'],$level)))
		{
			$this->data['error_messages']['info#con#end_credit']	= array('both'=>'无效的起始信用和最高信用');
		}
		else
		{
			if($level[$data['info']['con']['start_credit']]>=$level[$data['info']['con']['end_credit']])
			{
				$this->data['error_messages']['info#con#end_credit']	= array('lower'=>'起始信用必须小于最高信用');
			}
		}
		if(!empty($this->data['error_messages']))
		{
			$this->code	= 407;
		}
		else
		{
			if($data['info']['con']['guarantee']!='true')
			{
				$data['info']['con']['guarantee']	= 'false';
			}
			
			$productService		= new ProductService();
			$con				= $data['info']['con'];
			$con['page_no']		= 1;
			if(!array_key_exists('page_size',$con) || !is_numeric($con['page_size']) || $con['page_size']<0 || $con['page_size']>40)
			{
				$con['page_size']	= 20;
			}
			$cols				= array('iid','num_iid','title','nick','pic_url','price','click_url');
			$productService->searchTaoKeProducts($con,$cols,false);
			$productList	= $productService->getProductList();
			if($productList)
			{
				foreach($productList as &$plv)
				{
					$plv['title']	= strip_tags($plv['title']);
				}
			}
			$moduleService	= new ModuleService();
			
			if(is_numeric($id) && $id)
			{
				$data['data']	= $productList;
				$moduleService->update($id,$data);
				$this->code	= 200;
				$this->data['forward'] = C('LtUrl')->generate('Index', 'Index',array('position'=>$data['position']));
			}
			else
			{
				$id	= $moduleService->add($data);
				if($id)
				{
					//更新数据
					$moduleService->update($id,array('data'=>$productList));
					
					$this->code	= 200;
					$this->data['forward'] = C('LtUrl')->generate('Index', 'Index',array('position'=>$data['position']));
				}
				else
				{
					$this->code	= 400;
				}
			}
		}
	}
}
